<?php

class ForetellForm extends CFormModel {

    public $package;
    public $price;
    public $price_minimum;
    public $foretell_type;
    public $foretell_type_1;
    public $foretell_type_2;
    public $foretell_type_3;
    public $foretell_type_4;
    public $foretell_type_5;
    public $foretell_type_6;
    public $foretell_type_7;
    public $foretell_type_8;
    public $foretell_type_9;
    public $foretell_type_10;
    public $foretell_type_11;
    public $foretell_type_12;
    public $foretell_type_13;
    public $foretell_type_14;
    public $foretell_type_15;
    public $foretell_type_16;
    public $foretell_type_17;
    public $foretell_type_18;
    public $foretell_type_19;
    public $foretell_type_20;
    public $foretell_type_21;
    public $type;
    public $firstname;
    public $lastname;
    public $height;
    public $weight;
    public $gender;
    public $birthday;
    public $time_of_birthday;
    public $mobilephone;
    public $profile_image;
    public $remark;

    /**
     * Declares the validation rules.
     * The rules state that username and password are required,
     * and password needs to be authenticated.
     */
    public function rules() {
        return array(
            // username and password are required
            array('package, price, type', 'required'),
            array('price', 'checkMinimum'),
            array('firstname, lastname, gender, birthday, mobilephone', 'checkRequired'),
            // numeric with decimal only
            array('height', 'match', 'pattern' => '/^[0-9]{1,12}(\.[0-9]{0,4})?$/'),
            array('weight', 'match', 'pattern' => '/^[0-9]{1,12}(\.[0-9]{0,4})?$/'),
            array('mobilephone', 'numerical', 'integerOnly' => true),
            array('profile_image', 'checkProfileImageRequired'),
            array('profile_image', 'validate_order'),
        );
    }

    public function checkMinimum() {
        if ($this->price < $this->price_minimum) {
            $this->addError('price', Yii::t('foretell', 'price') . ' ' . Yii::t('foretell', 'check_minimum') . ' ' . $this->price_minimum);
        }
    }

    public function validate_order() {
        $count = 0;
        for ($i = 1; $i <= 5; $i++) {
            $val = $this["foretell_type_" . $i];
            if ($val != 0) {
                $count++;
            }
            if ($count > ForetellTypeUtil::MAXIMUM_OPTION_TYPE) {
                $this->addError('foretell_type', Yii::t('foretell', 'foretell_type') . ' ' . Yii::t('foretell', 'check_maximum') . ' ' . ForetellTypeUtil::MAXIMUM_OPTION_TYPE);
            }
        }
        
        if ($count == 0) {
            $this->addError('foretell_type', Yii::t('foretell', 'foretell_type') . ' ' . Yii::t('foretell', 'required'));
        }

        $order = Order::model()->countByAttributes(array(
            'cust_id' => Yii::app()->user->id,
            'status_id' => 1
        ));
        if ($order >= 1) {
            $this->addError('validate_order', Yii::t('foretell', 'validate_order'));
        }
    }

    public function checkProfileImageRequired() {
        if ($this->package == 3 || $this->package == 4) {
            $uploadedFile = CUploadedFile::getInstance($this, 'profile_image');
            if (!isset($uploadedFile)) {
                $this->addError('profile_image', Yii::t('register', 'profile_image') . ' ' . Yii::t('foretell', 'required'));
            }
        }
    }

    public function checkRequired($attribute, $params) {
        if ($this->type == 2) {
            if (empty($this[$attribute])) {
                $this->addError($attribute, Yii::t('register', $attribute) . ' ' . Yii::t('foretell', 'required'));
            }
        }
    }

    /**
     * Declares attribute labels.
     */
    public function attributeLabels() {
        return array(
            'package' => Yii::t('foretell', 'package'),
            'price' => Yii::t('foretell', 'price'),
            'type' => Yii::t('foretell', 'type'),
            'foretell_type' => Yii::t('foretell', 'foretell_type'),
            'firstname' => Yii::t('register', 'firstname'),
            'lastname' => Yii::t('register', 'lastname'),
            'height' => Yii::t('register', 'height'),
            'weight' => Yii::t('register', 'weight'),
            'gender' => Yii::t('register', 'gender'),
            'birthday' => Yii::t('register', 'birthday'),
            'time_of_birthday' => Yii::t('register', 'time_of_birthday'),
            'mobilephone' => Yii::t('register', 'mobilephone'),
            'profile_image' => Yii::t('register', 'profile_image'),
            'remark' => Yii::t('foretell', 'remark'),
        );
    }

    public function foretell() {
        $order = new Order();
        $order->cust_id = Yii::app()->user->id;
        $order->status_id = 1;
        $order->package_id = $this->package;
        $order->product_id = $this->package;

        $option_type = '';
        for ($i = 1; $i <= 5; $i++) {
            $val = $this["foretell_type_" . $i];
            if ($val != 0) {
                $option_type .= $val . ':';
            }
        }

        $option_type = substr($option_type, 0, strlen($option_type) - 1);

        $order->option_type = $option_type;
        $order->price = $this->price;
        $order->remark = $this->remark;
        $order->order_date = date('Y-m-d H:i:s');
        $order->create_date = date('Y-m-d H:i:s');
        $order->update_date = date('Y-m-d H:i:s');

        if ($this->package == 3 || $this->package == 4) {
            $uploadedFile = CUploadedFile::getInstance($this, 'profile_image');
            if (isset($uploadedFile)) {
//                $filename = substr($uploadedFile->name, 0, strrpos($uploadedFile->name, '.'));
                $filename = ImageUtil::PREFIX_FRIEND;
                $filepath = 'userimages/customer/' . date('Y-m-d_his') . $filename . '.' . $uploadedFile->extensionName;
                $order->picture = $filepath;
            }
        }

        if ($this->type == 1) {
            $cust = Customer::model()->findByAttributes(array('id' => Yii::app()->user->id));
            $order->name = $cust->Name;
            $order->surname = $cust->Surname;
            $order->height = $cust->height;
            $order->weight = $cust->weight;
            $order->sex = $cust->sex;
            $order->birthdate = $cust->birthdate;
            $order->contact_tel = $cust->telephone;
//            $order->picture = $cust->picture;

            if ($order->save()) {
                if (!empty($uploadedFile)) {  // check if uploaded file is set or not
                    $uploadedFile->saveAs($filepath);
                }
                return true;
            }
        } else {
            $order->name = $this->firstname;
            $order->surname = $this->lastname;
            $order->height = $this->height;
            $order->weight = $this->weight;
            $order->sex = $this->gender;
            $order->birthdate = ConvertFormatUtil::BEStringToADString($this->birthday, false);
            if (isset($this->time_of_birthday)) {
                $order->birthdate .= ' ' . $this->time_of_birthday;
            } else {
                $order->birthdate .= ' 00:00:00';
            }
            $order->contact_tel = $this->mobilephone;

            if ($order->save()) {
                if (!empty($uploadedFile)) {  // check if uploaded file is set or not
                    $uploadedFile->saveAs($filepath);
                }
                return true;
            }
        }
        return false;
    }

}
